package com.yisu.auction.manager.website.controller.systemConfig;

import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.AppSignManager;
import com.yisu.auction.ewf.entity.SystemConfig;
import com.yisu.auction.manager.core.service.IAppSignService;
import com.yisu.auction.manager.core.service.ISystemConfigService;
import com.yisu.auction.manager.domain.request.systemConfigPackage.SystemConfigRequest;
import com.yisu.auction.manager.website.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.net.URLDecoder;
import java.util.List;

/**
 * author: 黄萧林
 * Business Description：
 * createTime：2017/8/24
 */
@Controller
@RequestMapping(value = "/system")
public class systemConfigController extends BaseController {

    @Resource
    private ISystemConfigService iSystemConfigService;

    @Resource
    private IAppSignService iAppSignService;


    /**
     * author:黄萧林
     * Business Description:分页返回系统配置信息
     * @param model
     * @param systemConfigRequest
     * @return
     */
    @RequestMapping(value = "/index")
    public String index(Model model, SystemConfigRequest systemConfigRequest){
        try {
            if (systemConfigRequest.getKeyword() != null) {
                systemConfigRequest.setKeyword(URLDecoder.decode(systemConfigRequest.getKeyword(), "utf-8"));
            }
            EasyUIGrid<SystemConfig> systemConfigEasyUIGrid =iSystemConfigService.getSystemConfigInfoByPage(systemConfigRequest);
            model.addAttribute("systemConfigVos",systemConfigEasyUIGrid.getRows());
            model.addAttribute("currentPage",systemConfigRequest.getPage());
            model.addAttribute("totalPages",(systemConfigRequest.getTotalRecords() / systemConfigRequest.getRows()) + 1);
            model.addAttribute("keyword",systemConfigRequest.getKeyword());
        }catch (Exception e){
            logger.error("获取系统配置列表页面" + e.getMessage());
        }
        return "/system/systemConfig/index";
    }


    /**
     * 删除信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            if (iSystemConfigService.deleteSystemConfigInfo(id)) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }


    /**
     * author:黄萧林
     * Business Description:进入修改页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/modify.htm")
    public String modify(Long id,Model model){
        try{
            SystemConfig systemConfig = iSystemConfigService.getSystemConfig(id);
            List<AppSignManager> appSignManagers=iAppSignService.getAppSignManagerList();
            model.addAttribute("systemConfig",systemConfig);
            model.addAttribute("appSignManagers",appSignManagers);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "/system/systemConfig/modify";
    }

    /**
     * author:黄萧林
     * Business Description:修改信息
     * @param systemConfig
     * @return
     */
    @RequestMapping(value = "/modifyMessage.json")
    @ResponseBody
    public JsonResult modifyMessage(SystemConfig systemConfig){
        try {
            if (iSystemConfigService.modifySystemConfigInfo(systemConfig)) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }
}
